﻿using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.Windows.Forms;
using WinHBM.Utils;

namespace HBMDoc
{
    public partial class FormMain : Form
    {

        string page_url;
        int currentPage = 1, totalPage = 0;

        Form loginForm;

        public FormMain()
        {
            InitializeComponent();
        }

        public FormMain(Form loginForm):this()
        {
            this.loginForm = loginForm;
        }


        private void FormMain_Load(object sender, EventArgs e)
        {
            this.splitContainer2.Panel2.Hide();
            try
            {
                InitTreeView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        

        private  void InitTreeView()
        {
            JObject result = UtilRequest.DoRequest("https://hbmapi.jyq365.com/api/user-role-doc/list", UtilRequest.RequestMethod.GET, null);
            int errcode = result.Value<int>("errcode");
            
            if (errcode==0)
            {
                //root
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "全部文档";
                this.treeView.Nodes.Add(rootNode);

                JArray contents = result.Value<JArray>("contents");
                for (int i=0; i<contents.Count;i++)
                {

                    JObject itemJObject = (JObject)contents[i];
                    string name_1 = itemJObject.Value<string>("name");
                    JArray childrenJArray_1 = itemJObject.Value<JArray>("children");
                    TreeNode node_tree_1 = new TreeNode();
                    node_tree_1.Text = name_1;

                    for (int j=0; j< childrenJArray_1.Count;j++)
                    {

                        string name_2 = childrenJArray_1[j].Value<string>("name");
                        JObject childrenJObject = childrenJArray_1[j].Value<JObject>("children");

                        TreeNode node_tree_2 = new TreeNode();
                        node_tree_2.Text = name_2;

                        TreeNode ppt_Node = new TreeNode();
                        ppt_Node.Text = "PPT";
                        ppt_Node.Tag = childrenJObject.Value<string>("PPT")+"#"+ childrenJObject.Value<int>("ppt_page_size").ToString();
                        node_tree_2.Nodes.Add(ppt_Node);

                        TreeNode jiaoan_Node = new TreeNode();
                        jiaoan_Node.Text = "教案";
                        jiaoan_Node.Tag = childrenJObject.Value<string>("教案") + "#" + childrenJObject.Value<int>("jiaoan_page_size").ToString();
                        node_tree_2.Nodes.Add(jiaoan_Node);

                        node_tree_1.Nodes.Add(node_tree_2);
                    }
                    
                    rootNode.Nodes.Add(node_tree_1);
                }
            }


            treeView.AfterSelect += TreeView1_AfterSelect;
            if(treeView.Nodes.Count > 0)
            {
                treeView.Nodes[0].Expand();
            }
            
        }


        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse)
            {
              
                if (e.Node.Nodes.Count != 0)
                {
                    return;
                }
                string tag = e.Node.Tag.ToString();
                int totalPage = Convert.ToInt32(tag.Split("#".ToCharArray())[1]);
                if(totalPage == 0)
                {
                    MessageBox.Show("该节点没有数据");
                    return;
                }
                this.totalPage = totalPage;
                //说明当前选中节点没有子节点
                this.splitContainer2.Panel2.Show();
                currentPage = 1;
                txtPage.Text = "";
                showImage(tag);
                setPageNumber(currentPage, totalPage);
            }
        }

        private void showImage(string tag)
        {
            page_url = tag.Split("#".ToCharArray())[0];
            showImage();
        }

        private void showImage()
        {
            if (string.IsNullOrEmpty(page_url))  return;
            // this.pictureBox.ImageLocation = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537955945337&di=d2e93d3b42c12803857d701efd2db3c4&imgtype=0&src=http%3A%2F%2Fsudasuta.com%2Fwp-content%2Fuploads%2F2018%2F09%2Fsudasuta_Sergey-Shatskov23.jpg";

            System.Net.WebRequest webreq = System.Net.WebRequest.Create(page_url+ "/" + currentPage + ".jpg");
            System.Net.WebResponse webres = webreq.GetResponse();
            System.IO.Stream stream = webres.GetResponseStream();
            Image map = Image.FromStream(stream);
            pictureBox.Image = (Bitmap)map;

            //this.pictureBox.ImageLocation = page_url + "/" + currentPage + ".jpg";
        }

        /**
         * 上一页
         */
        private void button_next_Click(object sender, EventArgs e)
        {
            if (currentPage <= 1)
            {
                MessageBox.Show("已经第一页");
                return;
            }
            currentPage--;
            txtPage.Text = "";
            showImage();
            setPageNumber(currentPage,totalPage);
        }

        /**
         * 下一页
         */ 
        private void button_previous_Click(object sender, EventArgs e)
        {
            if (currentPage >= totalPage)
            {
                MessageBox.Show("已经最后一页");
                return;
            }

            currentPage++;
            txtPage.Text = "";
            showImage();
            setPageNumber(currentPage, totalPage);

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.loginForm.Close();
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.loginForm.Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAbout about = new FormAbout();
            about.ShowDialog();
        }

        private void btnJump_Click(object sender, EventArgs e)
        {
            try
            {
                int page = Convert.ToInt32(this.txtPage.Text);
                if (page <= 0 || page > totalPage)
                {
                    MessageBox.Show("输入的数据不合法");
                    return;
                }

                currentPage = page;
                showImage();
                setPageNumber(currentPage, totalPage);
            }
            catch (Exception)
            {
                MessageBox.Show("输入的数据不合法");
            }
            
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            
        }

        protected void setPageNumber(int current,int total)
        {
            this.label1.Text = String.Format("第{0}页/共{1}页",current,total);
        }

    }
}
